home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / groff108.lha / groff-1.08 / libgroff / strtol.c < prev    next >
C/C++ Source or Header  |  1992-08-03  |  3KB  |  121 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  2.      Written by James Clark (jjc@jclark.com)
  3.  
  4. This file is part of groff.
  5.  
  6. groff is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 2, or (at your option) any later
  9. version.
  10.  
  11. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License along
  17. with groff; see the file COPYING.  If not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #ifndef errno
  24. extern int errno;
  25. #endif
  26.  
  27. #ifdef HAVE_LIMITS_H
  28. #include <limits.h>
  29. #endif
  30.  
  31. #ifndef LONG_MAX
  32. #define LONG_MAX  2147483647
  33. #endif
  34.  
  35. #ifndef LONG_MIN
  36. #define LONG_MIN (-LONG_MAX-1)
  37. #endif
  38.  
  39. long strtol(str, ptr, base)
  40.      char *str, **ptr;
  41.      int base;
  42. {
  43.   char *start = str;
  44.   int neg = 0;
  45.   long val;
  46.   char *p;
  47.   static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  48.  
  49.   while (isascii(*str) && isspace(*str))
  50.     str++;
  51.  
  52.   if (*str == '-') {
  53.     neg = 1;
  54.     str++;
  55.   }
  56.   if (base == 0) {
  57.     if (*str == '0') {
  58.       if (str[1] == 'x' || str[1] == 'X') {
  59.     str += 2;
  60.     base = 16;
  61.       }
  62.       else
  63.     base = 8;
  64.     }
  65.     else
  66.       base = 10;
  67.   }
  68.   if (base < 2 || base > 36)
  69.     base = 10;
  70.   else if (base == 16 && *str == '0' && (str[1] == 'x' || str[1] == 'X'))
  71.     str += 2;
  72.  
  73.   p = strchr(digits, isascii(*str) && isupper(*str) ? tolower(*str) : *str);
  74.   if (p == 0 || (val = (p - digits)) >= base) {
  75.     if (base == 16 && str > start && (str[-1] == 'x' || str[-1] == 'X')) {
  76.       if (ptr)
  77.     *ptr = str - 1;
  78.     }
  79.     else {
  80.       if (ptr)
  81.     *ptr = start;
  82.       errno = ERANGE;
  83.     }
  84.     return 0;
  85.   }
  86.   if (neg)
  87.     val = -val;
  88.     
  89.   while (*++str != '\0') {
  90.     int n;
  91.  
  92.     p = strchr(digits, isascii(*str) && isupper(*str) ? tolower(*str) : *str);
  93.     if (p == 0)
  94.       break;
  95.     n = p - digits;
  96.     if (n >= base)
  97.       break;
  98.     if (neg) {
  99.       if (-(unsigned long)val > (-(unsigned long)LONG_MIN - n)/base) {
  100.     val = LONG_MIN;
  101.     errno = ERANGE;
  102.       }
  103.       else
  104.     val = val*base - n;
  105.     }
  106.     else {
  107.       if (val > (LONG_MAX - n)/base) {
  108.     val = LONG_MAX;
  109.     errno = ERANGE;
  110.       }
  111.       else
  112.     val = val*base + n;
  113.     }
  114.   }
  115.   
  116.   if (ptr)
  117.     *ptr = str;
  118.  
  119.   return val;
  120. }
  121.